package planit.controller.impl;

import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import planit.model.Event;
import planit.model.Model;
import planit.view.EventDialog;

@SuppressWarnings("serial")
public class CreateEventController extends AbstractAction {
	private Model model;

	public CreateEventController(Model model) {
		super();
		this.model = model;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		EventDialog dialog = (EventDialog)this.getValue("EventDialog");

		if (dialog.getEventName().length() > 0) {
			Event e = new Event();
			e.setDescription(dialog.getEventDescription());
			e.setEventType(dialog.getEventType());
			e.setName(dialog.getEventName());
			e.setPlace(dialog.getEventPlace());
			Calendar cal1 = new GregorianCalendar();
			cal1.setTime(dialog.getEventDate());
			Calendar cal2 = new GregorianCalendar();
			cal2.setTime(dialog.getEventStartTime());
			cal1.set(Calendar.HOUR_OF_DAY, cal2.get(Calendar.HOUR_OF_DAY));
			cal1.set(Calendar.MINUTE, cal2.get(Calendar.MINUTE));
			e.setStartTime(cal1.getTime());
			cal1.setTime(dialog.getEventDuration());
			e.setDuration((cal1.get(Calendar.HOUR_OF_DAY) * 60)
					+ cal1.get(Calendar.MINUTE));
			model.getSchedule().addEvent(e);
			dialog.dispose();
		} else {
			JOptionPane.showMessageDialog(null,
					"You have to enter the name of the event", "Error",
					JOptionPane.WARNING_MESSAGE);
		}
	}
}
